<?php

namespace app\controller;

use think\facade\Request;
use app\BaseController;
use app\ResourceModel;
use app\controller\ResourceController;
use think\facade\Db;

class ResourceCategoryController extends ResourceController
{
//获取分类列表
    public function getCategoryList(Request $request)
    {
        $param = $request::param();
        $where = [];
        if (!empty($param['title'])) {
            $where['title'] = $param['title'];
        }
        $list = Db::table('article_category')
            ->where('del_flag', 0)
            ->where($where)
            ->order('sort', 'desc')
            ->select()->toArray();
        return $this->returnJson(1001, '返回成功', ['list' => $list]);

    }

//添加分类
    public function addCategory(Request $request)
    {
        $param = $request::post();
        $data['id'] = uniqid();
        $data['title'] = $param['title'];
        $data['create_time'] = toDate(time());
        $ret = Db::table('article_category')->insert($data);
        if ($ret != false) {
            return $this->returnJson(1001, '保存成功', $param);
        } else {
            return $this->returnJson(1002, '保存失败', ['data' => $data]);
        }
    }

    //编辑分类
    public function editCategory(Request $request)
    {
        $param = $request::post();
        $data['title'] = $param['title'];
        $data['create_time'] = $param['create_time'];
        $ret = Db::table('article_category')->where('id', $param['id'])->update($data);
        if ($ret != false) {
            return $this->returnJson(1001, '修改成功', $param);
        } else {
            return $this->returnJson(1002, '修改失败');
        }
    }

    //获取分类名称列表
    public function getCategoryNameList(Request $request)
    {
        $param = $request::param();
        $list = Db::table('article_category')->where('del_flag', 0)->field('title')->select()->toArray();
        return $this->returnJson(1001, '返回成功', ['list' => $list]);

    }
//通过id获取分类列表
    public function getCategoryById(Request $request)
    {
        $param = $request::param('id');
        $list = Db::table('article_category')->where('del_flag', 0)->where('id', $param)->select()->toArray();
        return $this->returnJson(1001, '返回成功', ['list' => $list]);

    }
//删除单条
    public function deleteCategory(Request $request)
    {
        $param = $request::param();
        $result = Db::table('article_category')->where('id', $param['id'])->update(['del_flag' => 1]);
        if ($result != false) {
            return $this->returnJson(1001, '删除成功');
        } else {
            return $this->returnJson(1002, '删除失败');
        }
    }
    //批量删除
    public function deleteCategoryStrips(Request $request)
    {
        $param = $request::param();
        $result = Db::table('article_category')->whereIn('id', json_decode($param['ids']))->update(['del_flag' => 1]);
        if ($result != false) {
            return $this->returnJson(1001, '删除成功');
        } else {
            return $this->returnJson(1002, '删除失败');
        }
    }
}
